//
//  UIColor+IHGradientChange.m
//  HWan94SDK
//
//  Created by 陈道斌 on 2020/4/13.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "UIColor+IHGradientChange.h"




@implementation UIColor (IHGradientChange)



+ (instancetype)bm_colorGradientChangeWithSize:(CGSize)size
                                     direction:(IHGradientChangeDirection)direction
                                    startColor:(UIColor *)startcolor
                                      endColor:(UIColor *)endColor {
    
    if (CGSizeEqualToSize(size, CGSizeZero) || !startcolor || !endColor) {
        return nil;
    }
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);

    CGPoint startPoint = CGPointZero;
    if (direction == IHGradientChangeDirectionDownDiagonalLine) {
        startPoint = CGPointMake(0.0, 1.0);
    }
    gradientLayer.startPoint = startPoint;
    
    CGPoint endPoint = CGPointZero;
    switch (direction) {
        case IHGradientChangeDirectionLevel:
            endPoint = CGPointMake(1.0, 0.0);
            break;
        case IHGradientChangeDirectionVertical:
            endPoint = CGPointMake(0.0, 1.0);
            break;
        case IHGradientChangeDirectionUpwardDiagonalLine:
            endPoint = CGPointMake(1.0, 1.0);
            break;
        case IHGradientChangeDirectionDownDiagonalLine:
            endPoint = CGPointMake(1.0, 0.0);
            break;
        default:
            break;
    }
    gradientLayer.endPoint = endPoint;
    
    gradientLayer.colors = @[(__bridge id)startcolor.CGColor, (__bridge id)endColor.CGColor];
    UIGraphicsBeginImageContext(size);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return [UIColor colorWithPatternImage:image];
    
    
}


+ (instancetype)bm_colorGradientChangeWithSizeView:(UIView *)view {
    
    
    
    
    CGSize size = view.size;
    
    IHGradientChangeDirection  direction = IHGradientChangeDirectionLevel;
    
    // 浅色
    UIColor  *startcolor =QCqiansetupian;
    
    // 深色
    UIColor *endColor = QCshensetupian;
    
    //纯色
     // 248 119  6
    
    
    
    
    
    
    if (CGSizeEqualToSize(size, CGSizeZero) || !startcolor || !endColor) {
         return nil;
     }
    
    
    
     
     CAGradientLayer *gradientLayer = [CAGradientLayer layer];
     gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);

     CGPoint startPoint = CGPointZero;
     if (direction == IHGradientChangeDirectionDownDiagonalLine) {
         startPoint = CGPointMake(0.0, 1.0);
     }
     gradientLayer.startPoint = startPoint;
     
     CGPoint endPoint = CGPointZero;
     switch (direction) {
         case IHGradientChangeDirectionLevel:
             endPoint = CGPointMake(1.0, 0.0);
             break;
         case IHGradientChangeDirectionVertical:
             endPoint = CGPointMake(0.0, 1.0);
             break;
         case IHGradientChangeDirectionUpwardDiagonalLine:
             endPoint = CGPointMake(1.0, 1.0);
             break;
         case IHGradientChangeDirectionDownDiagonalLine:
             endPoint = CGPointMake(1.0, 0.0);
             break;
         default:
             break;
     }
     gradientLayer.endPoint = endPoint;
     
     gradientLayer.colors = @[(__bridge id)startcolor.CGColor, (__bridge id)endColor.CGColor];
     UIGraphicsBeginImageContext(size);
     [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
     UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     return [UIColor colorWithPatternImage:image];
    
    
    
    
    
}


+ (instancetype)bm_fulicolorGradientChangeWithSizeView:(UIView *)view {
    
    
    CGSize size = view.size;
      
      IHGradientChangeDirection  direction = IHGradientChangeDirectionLevel;
      
      // 浅色
      UIColor  *startcolor = RGB(73, 74, 86);
      
      // 深色
      UIColor *endColor = RGB(32 , 32, 42);
      
      //纯色
       // 248 119  6
      
      
      
      
      
      
      if (CGSizeEqualToSize(size, CGSizeZero) || !startcolor || !endColor) {
           return nil;
       }
      
      
      
       
       CAGradientLayer *gradientLayer = [CAGradientLayer layer];
       gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);

       CGPoint startPoint = CGPointZero;
       if (direction == IHGradientChangeDirectionDownDiagonalLine) {
           startPoint = CGPointMake(0.0, 1.0);
       }
       gradientLayer.startPoint = startPoint;
       
       CGPoint endPoint = CGPointZero;
       switch (direction) {
           case IHGradientChangeDirectionLevel:
               endPoint = CGPointMake(1.0, 0.0);
               break;
           case IHGradientChangeDirectionVertical:
               endPoint = CGPointMake(0.0, 1.0);
               break;
           case IHGradientChangeDirectionUpwardDiagonalLine:
               endPoint = CGPointMake(1.0, 1.0);
               break;
           case IHGradientChangeDirectionDownDiagonalLine:
               endPoint = CGPointMake(1.0, 0.0);
               break;
           default:
               break;
       }
       gradientLayer.endPoint = endPoint;
       
       gradientLayer.colors = @[(__bridge id)startcolor.CGColor, (__bridge id)endColor.CGColor];
       UIGraphicsBeginImageContext(size);
       [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
       UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();
       return [UIColor colorWithPatternImage:image];
    
    
    
    
    
}



+ (instancetype)bm_colorGradientChangeWithSizeView:(UIView *)view kaish:(UIColor *)kaishi endjishu:(UIColor *)jieshu {
    
    
    CGSize size = view.size;
      
      IHGradientChangeDirection  direction = IHGradientChangeDirectionLevel;
      
      // 浅色
      UIColor  *startcolor =kaishi;
      
      // 深色
      UIColor *endColor = jieshu;
      
      //纯色
       // 248 119  6
      
      
      
      
      
      
      if (CGSizeEqualToSize(size, CGSizeZero) || !startcolor || !endColor) {
           return nil;
       }
      
      
      
       
       CAGradientLayer *gradientLayer = [CAGradientLayer layer];
       gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);

       CGPoint startPoint = CGPointZero;
       if (direction == IHGradientChangeDirectionDownDiagonalLine) {
           startPoint = CGPointMake(0.0, 1.0);
       }
       gradientLayer.startPoint = startPoint;
       
       CGPoint endPoint = CGPointZero;
       switch (direction) {
           case IHGradientChangeDirectionLevel:
               endPoint = CGPointMake(1.0, 0.0);
               break;
           case IHGradientChangeDirectionVertical:
               endPoint = CGPointMake(0.0, 1.0);
               break;
           case IHGradientChangeDirectionUpwardDiagonalLine:
               endPoint = CGPointMake(1.0, 1.0);
               break;
           case IHGradientChangeDirectionDownDiagonalLine:
               endPoint = CGPointMake(1.0, 0.0);
               break;
           default:
               break;
       }
       gradientLayer.endPoint = endPoint;
       
       gradientLayer.colors = @[(__bridge id)startcolor.CGColor, (__bridge id)endColor.CGColor];
       UIGraphicsBeginImageContext(size);
       [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
       UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();
       return [UIColor colorWithPatternImage:image];
      
    
}


@end
